package com.usian.admin.service.impl;





import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.common.BaseServiceimpl;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;


@Service
public class AdChannelServiceImpl extends BaseServiceimpl<AdChannelMapper,AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        if(dto==null) return errorResult;
        Page pageDto = new Page(dto.page, dto.size);
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        IPage page = page(pageDto,lambdaQueryWrapper);
        ResponseResult/*<List<AdChannel>> */responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        if (channel==null||channel.getId()==null) return errorResult;
        updateById(channel);
        return sucResult;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null) return errorResult;
        AdChannel byId = getById(id);
        if (byId==null)
            return noData;
        if (byId.getStatus().equals(0))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        removeById(id);
        return sucResult;
    }
}
